package com.tb.market;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.tb.coremodel.datamodel.http.entities.MarketData;
import com.tb.coremodel.viewmodel.MarketViewModel;

import com.tb.common.base.ARouterPath;
import com.tb.common.base.BaseActivity;
import com.tb.coremodel.viewmodel.ViewModelProviders;
import com.tb.market.databinding.ActivityMarketBinding;

@Route(path = ARouterPath.MarketListAty)
public class ActivityMarket extends BaseActivity {

    MarketAdapter marketAdapter;
    ActivityMarketBinding activityMarketBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("期货行情");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        activityMarketBinding = DataBindingUtil.setContentView(ActivityMarket.this, R.layout.activity_market);
        MarketViewModel marketViewModel =
        ViewModelProviders.of(ActivityMarket.this).get(MarketViewModel.class);
        marketAdapter = new MarketAdapter(marketItemClickCallback);
        activityMarketBinding.setRecyclerAdapter(marketAdapter);
        subscribeToModel(marketViewModel);

    }

    MarketItemClickCallback marketItemClickCallback = new MarketItemClickCallback() {
        @Override
        public void onClick(MarketData.ResultsBean fuliItem) {
            Log.d("Clark",new Gson().toJson(fuliItem));
            Toast.makeText(ActivityMarket.this, fuliItem.getDesc(), Toast.LENGTH_SHORT).show();
            if("001".equals(fuliItem.get_id())){
                ARouter.getInstance()
                        .build(ARouterPath.DynaMarketListAty)
                        .withTransition(R.anim.activity_up_in, R.anim.activity_up_out)
                        .navigation(ActivityMarket.this);
            }else if("002".equals(fuliItem.get_id())){
                ARouter.getInstance()
                        .build(ARouterPath.NewsListAty)
                        .withTransition(R.anim.activity_up_in, R.anim.activity_up_out)
                        .navigation(ActivityMarket.this);
            }
        }
    };

    /**
     * 订阅数据变化来刷新UI
     * @param model
     */
    private void subscribeToModel(final MarketViewModel model){
        //观察数据变化来刷新UI
        model.getLiveObservableData().observe(this, new Observer<MarketData>() {
            @Override
            public void onChanged(@Nullable MarketData marketData) {
                Log.i("clark", "subscribeToModel onChanged onChanged");
                model.setUiObservableData(marketData);
                marketAdapter.setMarketList(marketData.getResults());
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
